
let utils = require("../../../utils/util.js");
let tcity = require("../../../utils/cityData.js");
let tBizType = require("../../../utils/bizTypeData.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //页面遮罩开关
    maskCondition: false,
    /*城市选择所需数据字段 */
    provinces: [],
    citys: [],
    value: [0, 0, 0],
    values: [0, 0, 0],
    locationId: 0,//选择的城市ID
    locationName: "选择业务办理城市",
    condition: false,
    /*end 城市选择所需数据字段 */
    /*业务选择所需数据字段*/
    bizTypeGroups: [],
    bizTypes: [],
    bizTypeValue: [0, 0],
    bizTypeValues: [0, 0],
    bizTypeId: 0,//选择的业务ID
    bizTypeName: "选择业务类型",
    bizTypeCondition: false,
    /*end业务选择所需数据字段*/
    shopImgFullPaths: [],
    //最大图片上传个数
    maxImgsCount: 9,
    animationBodyCity: {},//地址动画主体存放
    animationBodyType: {},//选择类型动画
    animationMask: {}//遮罩动画
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      // 来自页面右上角转发按钮
      return {
        title: '快来上车务圈，更多人脉，更多订单！限时免费注册！',
        path: '/pages/index/index',
        imageUrl: '/imgs/share-banner.jpg'
      };
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //tcity.init(that);
    tBizType.init(that);

   // var cityData = that.data.cityData;
    var bizTypeData = that.data.bizTypeData;


    const provinces = tcity.provinceList();
    provinces.splice(0, 1);
    const citys = tcity.cityList(provinces[0].value);
    citys.splice(0, 1);
    // const provinces = [];
    // const citys = [];
    // for (let i = 0; i < cityData.length; i++) {
    //   provinces.push(cityData[i]);
    // }
    // for (let i = 0; i < cityData[0].childs.length; i++) {
    //   citys.push(cityData[0].childs[i])
    // }
    that.setData({
      'provinces': provinces,
      'citys': citys
    })

    const bizTypeGroups = [];
    const bizTypes = [];
    for (let i = 0; i < bizTypeData.length; i++) {
      bizTypeGroups.push(bizTypeData[i]);
    }
    for (let i = 0; i < bizTypeData[0].childs.length; i++) {
      bizTypes.push(bizTypeData[0].childs[i])
    }
    that.setData({
      bizTypeGroups: bizTypeGroups,
      bizTypes: bizTypes
    })

  },
  // 地址组件点击打开
  bindOpenLocationPicker: function () {
    // this.setData({
    //   condition: true,
    //   maskCondition: true
    // })
    let that = this;
    // 主体动画
    let animationBC = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animationBC = animationBC;
    animationBC.height('0px').step();
    that.setData({
      animationBodyCity: animationBC.export()
    })
    setTimeout(function () {
      animationBC.height("300px").step();
      that.setData({
        animationBodyCity: animationBC.export()
      })
    }.bind(that), 200);

    // 遮罩动画
    let animationM = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    });
    that.animationM = animationM;
    if (that.data.maskCondition) {
      animationM.opacity('0.5').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: false
      })
      setTimeout(function () {
        animationM.opacity('0').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    } else {
      animationM.opacity('0').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: true
      })
      setTimeout(function () {
        animationM.opacity('0.5').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    }
  },
  // 地址组件确定
  bindLocationPickerConfirm: function () {
    var that = this;
    var val = that.data.value;
    var provinces = that.data.provinces;
    var citys = that.data.citys;
    var szProvinceName = provinces[val[0]].text;
    var szCityName = citys[val[1]] != null ? citys[val[1]].text : "";

    var szLocationName = szProvinceName + "-" + szCityName;
    var locationId = citys[val[1]] != null ? citys[val[1]].value : 0;

    this.setData({
      // condition: false,
      // maskCondition: false,
      locationName: szLocationName,
      locationId: locationId
    });

    // 遮罩动画
    let animationM = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    });
    that.animationM = animationM;
    if (that.data.maskCondition) {
      animationM.opacity('0.5').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: false
      })
      setTimeout(function () {
        animationM.opacity('0').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    } else {
      animationM.opacity('0').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: true
      })
      setTimeout(function () {
        animationM.opacity('0.5').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    };

    // 主体盒子
    let animationBC = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animationBC = animationBC;
    animationBC.height('300px').step();
    that.setData({
      animationBodyCity: animationBC.export()
    })
    setTimeout(function () {
      animationBC.height("0px").step();
      that.setData({
        animationBodyCity: animationBC.export()
      })
    }.bind(that), 200);

  },
  // 地址组件取消
  bindLocationPickerCancel: function () {
    // this.setData({
    //   condition: false,
    //   maskCondition: false,
    // })

    let that = this;

    // 遮罩动画
    let animationM = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    });
    that.animationM = animationM;
    if (that.data.maskCondition) {
      animationM.opacity('0.5').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: false
      })
      setTimeout(function () {
        animationM.opacity('0').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    } else {
      animationM.opacity('0').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: true
      })
      setTimeout(function () {
        animationM.opacity('0.5').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    };

    // 主体盒子
    let animationBC = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animationBC = animationBC;
    animationBC.height('300px').step();
    that.setData({
      animationBodyCity: animationBC.export()
    })
    setTimeout(function () {
      animationBC.height("0px").step();
      that.setData({
        animationBodyCity: animationBC.export()
      })
    }.bind(that), 200);
  },
  bindLocationChange: function (e) {
    //console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    if (val[0] != t[0]) {
      console.log('province no ');
      var s = this.data.provinces;
      const citys = tcity.cityList(s[val[0]].value);
      citys.splice(0, 1);
      // const citys = [];
      // for (let i = 0; i < cityData[val[0]].childs.length; i++) {
      //   citys.push(cityData[val[0]].childs[i])
      // }
      this.setData({
        citys: citys,
        values: val,
        value: [val[0], 0, 0]
      })
      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      this.setData({
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }

  },
// 类型组件
  bindOpenBizTypePicker: function () {
    // this.setData({
    //   bizTypeCondition: true,
    //   maskCondition: true
    // })

    let that = this;

    // 主体动画
    let animationBT = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animationBT = animationBT;
    animationBT.height('0px').step();
    that.setData({
      animationBodyType: animationBT.export()
    })
    setTimeout(function () {
      animationBT.height("300px").step();
      that.setData({
        animationBodyType: animationBT.export()
      })
    }.bind(that), 200);

    // 遮罩动画
    let animationM = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    });
    that.animationM = animationM;
    if (that.data.maskCondition) {
      animationM.opacity('0.5').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: false
      })
      setTimeout(function () {
        animationM.opacity('0').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    } else {
      animationM.opacity('0').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: true
      })
      setTimeout(function () {
        animationM.opacity('0.5').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    }
  },
  // 类型组件确定按钮
  bindBizTypePickerConfirm: function () {
    var that = this;
    var val = that.data.bizTypeValue;
    var bizTypeGroups = that.data.bizTypeGroups;
    var bizTypes = that.data.bizTypes;
    var szBizTypeGroup = bizTypeGroups[val[0]].text;
    var szBizType = bizTypes[val[1]] != null ? bizTypes[val[1]].text : "";

    var szBizTypeName = szBizType;
    var bizTypeId = bizTypes[val[1]] != null ? bizTypes[val[1]].value : 0;

    this.setData({
      // bizTypeCondition: false,
      // maskCondition: false,
      bizTypeName: szBizTypeName,
      bizTypeId: bizTypeId
    });

    // 遮罩动画
    let animationM = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    });
    that.animationM = animationM;
    if (that.data.maskCondition) {
      animationM.opacity('0.5').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: false
      })
      setTimeout(function () {
        animationM.opacity('0').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    } else {
      animationM.opacity('0').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: true
      })
      setTimeout(function () {
        animationM.opacity('0.5').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    };

    // 主体盒子
    let animationBT = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animationBT = animationBT;
    animationBT.height('300px').step();
    that.setData({
      animationBodyType: animationBT.export()
    })
    setTimeout(function () {
      animationBT.height("0px").step();
      that.setData({
        animationBodyType: animationBT.export()
      })
    }.bind(that), 200);
  },
  // 类型组件取消
  bindBizTypePickerCancel: function () {
    // this.setData({
    //   bizTypeCondition: false,
    //   maskCondition: false,
    // })
    let that = this;

    // 遮罩动画
    let animationM = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    });
    that.animationM = animationM;
    if (that.data.maskCondition) {
      animationM.opacity('0.5').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: false
      })
      setTimeout(function () {
        animationM.opacity('0').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    } else {
      animationM.opacity('0').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: true
      })
      setTimeout(function () {
        animationM.opacity('0.5').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    };

    // 主体盒子
    let animationBT = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animationBT = animationBT;
    animationBT.height('300px').step();
    that.setData({
      animationBodyType: animationBT.export()
    })
    setTimeout(function () {
      animationBT.height("0px").step();
      that.setData({
        animationBodyType: animationBT.export()
      })
    }.bind(that), 200);

  },
  bindBizTypeChange: function (e) {
    var val = e.detail.value
    var t = this.data.bizTypeValues;
    var bizTypeData = this.data.bizTypeData;
    if (val[0] != t[0]) {
      const bizTypes = [];
      for (let i = 0; i < bizTypeData[val[0]].childs.length; i++) {
        bizTypes.push(bizTypeData[val[0]].childs[i])
      }
      this.setData({
        bizTypes: bizTypes,
        bizTypeValues: val,
        bizTypeValue: [val[0], 0]
      })
      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      this.setData({
        bizTypeValues: val,
        bizTypeValue: [val[0], val[1]]
      })
      return;
    }

  },
  bindChooseImg: function () {
    var that = this;
    var count = this.data.maxImgsCount - this.data.shopImgFullPaths.length;
    wx.chooseImage({
      count: count, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        for (let i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: utils.apiUrl + '/common/upload',
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {

            },
            success: function (res) {
              var data = JSON.parse(res.data);
              data = JSON.parse(data);
              var shopImgFullPaths = that.data.shopImgFullPaths;
              shopImgFullPaths.push(data);
              //do something
              that.setData({
                shopImgFullPaths: shopImgFullPaths
              });
            },
            fail: function (e) {
              wx.showToast({
                title: '上传图片失败',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  /**
 * 删除图片
 */
  catchDelImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var shopImgFullPaths = this.data.shopImgFullPaths;
    if (shopImgFullPaths.length > 0) {
      shopImgFullPaths.splice(index, 1);
      this.setData({
        shopImgFullPaths: shopImgFullPaths
      });
    }
  },
  // 遮罩点击
  bindMaskClick: function () {
    // this.setData({
    //   maskCondition: false,
    //   condition: false,
    //   bizTypeCondition: false
    // });
    let that = this;
    // 遮罩动画
    let animationM = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    });
    that.animationM = animationM;
    if (that.data.maskCondition) {
      animationM.opacity('0.5').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: false
      })
      setTimeout(function () {
        animationM.opacity('0').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    } else {
      animationM.opacity('0').step();
      that.setData({
        animationMask: animationM.export(),
        maskCondition: true
      })
      setTimeout(function () {
        animationM.opacity('0.5').step();
        that.setData({
          animationMask: animationM.export()
        })
      }.bind(that), 200);
    };

    // 主体盒子
    let animationBC = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animationBC = animationBC;
    animationBC.height('300px').step();
    that.setData({
      animationBodyCity: animationBC.export()
    })
    setTimeout(function () {
      animationBC.height("0px").step();
      that.setData({
        animationBodyCity: animationBC.export()
      })
    }.bind(that), 200);
    // 类型
    let animationBT = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animationBT = animationBT;
    animationBT.height('300px').step();
    that.setData({
      animationBodyType: animationBT.export()
    })
    setTimeout(function () {
      animationBT.height("0px").step();
      that.setData({
        animationBodyType: animationBT.export()
      })
    }.bind(that), 200);
  },
  bindFormSubmit: function (e) {
    var that = this;
    console.log(e);
    var formData = e.detail.value;
    if (that.data.bizTypeId == 0) {
      wx.showToast({
        title: '请选择业务类型',
        icon: 'none'
      });
      return;
    }
    if (that.data.locationId == 0) {
      wx.showToast({
        title: '请选择业务办理城市',
        icon: 'none'
      });
      return;
    }
    if (formData.PersonPrice == '') {
      wx.showToast({
        title: '请输入报价',
        icon: 'none'
      });
      return;
    }

    if (formData.Content.replace(/\s+/g, '')  == '') {
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      });
      return;
    }
    if (that.data.shopImgFullPaths.length <1) {
      wx.showToast({
        title: '请上传至少1张配图',
        icon: 'none'
      });
      return;
    }

    //build form data 
    var shopImgs = that.data.shopImgFullPaths.map(item => {
      return item.FileRelativeFullPath
    });
    formData.BizTypeID = that.data.bizTypeId;
    formData.LocationID = that.data.locationId;

    formData.PostImgs = shopImgs;

    wx.showLoading({
      title: '正在提交,请等待..',
    })
    utils.postData(
      utils.apiUrl + '/CarCircle/CreateReceiveOrder',
      formData,
      function (response) {
        if (response.status == 0) {
          var postID = response.data.postID;
          wx.switchTab({
            url: '../../carcircle/carcircle',
          });
        }
        else {
          wx.showModal({
            title: '操作失败',
            content: response.message,
            showCancel: false,
          });
        }
      },
      function () {

      },
      function () {
        wx.hideLoading();
      }
    );
  },
})